/*
 * 创建用户：mleo
 * 项目名称：demo
 * 模块名称：demo
 * 文件名称：BeanJson
 * 修改时间：2017/11/14 13:55:27
 * Copyright (c) 2017.
 */

package com.ichochy.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.util.Map;

public class BeanJson {

    public static Map<String, String> transBeanToMap(Object obj) {
        String json = JSON.toJSONString(obj, SerializerFeature.WriteNonStringValueAsString);
        Map<String, String> map = JSON.parseObject(json, new TypeReference<Map<String, String>>() {
        }.getType());
        return map;
    }

    public static <T> T transMapToBeans(Map<String, String> map, Class<T> clazz) {
        String json = JSON.toJSONString(map);
        return transJsonToBeans(json, clazz);
    }

    public static <T> T transJsonToBeans(String json, Class<T> clazz) {
        json = json.replaceAll("\\\\(?=\")|(\"(?=\\[\\{))|(?<=}])\"|\"(\\[])\"|\"\\w+\":(\"\")(,?)", ""); // (\)"|(")[|](")|("*":"")(,)
        T obj = JSON.parseObject(json, clazz);
        return obj;
    }

    public static void main(String[] args) {




    }



}
